package com.ionehe.group.common.uaa.ex.dto;

import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ForestNodeManager<T extends INode> {
    private List<T> list;
    private List<String> parentIds = new ArrayList();

    public ForestNodeManager(List<T> items) {
        this.list = items;
    }

    public INode getTreeNodeAT(String id) {
        Iterator var2 = this.list.iterator();

        while(var2.hasNext()) {
            INode forestNode = (INode)var2.next();
            if (StrUtil.equals(forestNode.getId(), id)) {
                return forestNode;
            }
        }

        return null;
    }

    public void addParentId(String parentId) {
        this.parentIds.add(parentId);
    }

    public List<T> getRoot() {
        List<T> roots = new ArrayList();
        Iterator var2 = this.list.iterator();

        while(true) {
            INode forestNode;
            do {
                if (!var2.hasNext()) {
                    return roots;
                }

                forestNode = (INode)var2.next();
                String var4 = forestNode.getParentId();
            } while((!StrUtil.equals(forestNode.getParentId(), "0") || !StrUtil.isEmpty(forestNode.getParentId())) && !this.parentIds.contains(forestNode.getId()));

            roots.add((T) forestNode);
        }
    }
}
